package com.changgou.goods.controller;

import com.changgou.framework.entity.PageResult;
import com.changgou.framework.entity.Result;
import com.changgou.framework.entity.StatusCode;
import com.changgou.goods.service.ParaService;
import com.haha.goods.entity.Para;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/7
 */
@RestController
@CrossOrigin
@RequestMapping("/para")
public class ParaController {
    @Autowired
    private ParaService paraService;

    /***
     * Para分页条件搜索实现
     * @param para
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result findPage(@RequestBody(required = false) Para para, @PathVariable  int page, @PathVariable  int size){
        //执行搜索
        PageResult pageInfo = paraService.findPage(para, page, size);
        return Result.ok(pageInfo);
    }
    /***
     * Para分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result findPage(@PathVariable  int page, @PathVariable  int size){
        //分页查询
        PageResult pageInfo = paraService.findPage(null,page, size);
        return Result.ok(pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param para
     * @return
     */
    @PostMapping(value = "/search" )
    public Result findList(@RequestBody(required = false)  Para para){
        List list = paraService.findPage(para,1,Integer.MAX_VALUE).getRows();
        return Result.ok(list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        boolean b = paraService.removeById(id);
        return b?Result.ok(null):Result.error("删除失败");
    }

    /***
     * 修改Para数据
     * @param para
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Para para,@PathVariable Integer id){
        //设置主键值
        para.setId(id);
        //修改数据
        boolean b = paraService.updateById(para);
        return b?Result.ok(null):Result.error("修改失败");
    }

    /***
     * 新增Para数据
     * @param para
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Para para){
        boolean b = paraService.save(para);
        return b?Result.ok(null):Result.error("新增失败");
    }

    /***
     * 根据ID查询Para数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        //根据ID查询
        Para para = paraService.getById(id);
        return Result.ok(para);
    }

    /***
     * 查询Para全部数据
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Para> list = paraService.list();
        return Result.ok(list);
    }
    /**
     * 根据分类ID查询参数列表
     * @param id
     * @return
     */
    @GetMapping(value = "/category/{id}")
    public Result getByCategoryId(@PathVariable(value = "id")Integer id){
        //根据分类ID查询对应的参数信息
        List<Para> paras = paraService.findByCategoryId(id);
        return Result.ok(paras);
    }


}
